package com.tianji.aigc.controller;


import com.tianji.aigc.domain.vo.ChatSessionVO;
import com.tianji.aigc.domain.vo.MessageVO;
import com.tianji.aigc.domain.vo.SessionVO;
import com.tianji.aigc.service.IChatSessionService;
import com.tianji.common.utils.UserContext;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 对话session 前端控制器
 * </p>
 *
 * @author lzy
 * @since 2025-10-13
 */
@Slf4j
@RestController
@RequestMapping("/session")
@RequiredArgsConstructor
public class ChatSessionController {

    private final IChatSessionService chatSessionService;

    /**
     * 新建会话
     */
    @PostMapping
    public SessionVO createSession(@RequestParam(value = "n",defaultValue = "3") Integer num) {
        return chatSessionService.createSession(num);
    }

    /**
     * 热门问题
     */
    @GetMapping("/hot")
    public List<SessionVO.Example> getHotQuestions(@RequestParam(value = "n",defaultValue = "3") Integer num) {
        return chatSessionService.getHotQuestions(num);
    }

    /**
     * 查询单个历史对话详情
     *
     * @return 对话记录列表
     */
    @GetMapping("/{sessionId}")
    public List<MessageVO> queryBySessionId(@PathVariable("sessionId") String sessionId) {
        return this.chatSessionService.queryBySessionId(sessionId);
    }

    /**
     * 查询所有历史对话
     * @return 所有历史对话
     */
    @GetMapping("/history")
    public Map<String, List<ChatSessionVO>> queryHistory() {
        return chatSessionService.queryHistory();
    }

    /**
     * 删除历史对话
     * @param sessionId 会话id
     */
    @DeleteMapping("/history")
    public void deleteHistory(@RequestParam("sessionId") String sessionId) {
        chatSessionService.removeHistory(sessionId);
    }

    /**
     * 更新历史对话
     * @param sessionId 会话id
     * @param title 新的会话标题
     */
    @PutMapping("/history")
    public void updateHistory(@RequestParam("sessionId") String sessionId,
                             @RequestParam("title") String title) {
        chatSessionService.updateHistory(sessionId, title);
    }
}
